package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.comboBox.ComboBox;
	import com.fairytask.engine.components.comboBox.ComboBoxEvent;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.events.MouseEvent;
	
	/**
	 * 万灵塔系统界面
	 * @author pangj
	 * 
	 */
	public class MillionStupaUI extends FrameUI
	{ 
		private static var _self  :MillionStupaUI = null;
		private var _model         :Modulator = Modulator.instance;	
		private var loseGoods      :Array;      //掉落的物品
		private var towerLevel     :int;        //当前可挑战的最高层数
		private var nowChoose      :int;        //当前所在层数
		//窗体控件--
		public var stupaBG         :Clip;       //窗口头部
		public var imgWinBG        :Window;     //怪头像图片窗口
		public var challengeBtn    :Button;     //挑战按钮
		public var challengeImg    :Clip;       //挑战怪的头像
		public var challengeTim    :Clip;       //剩余挑战次数标签
		public var challengeNum    :Label;      //挑战次数
		public var loseGoodsBG     :Window;     //掉落物品窗口
		public var loseGoodsLabel  :Clip;       //掉落物品标签
		public var chooseFloor     :ComboBox;   //万灵塔楼层选择框
		public var isWin           :Array = []; //是否挑战成功
		public var isWinImg        :Clip;       //胜利图片
			
		/**
		 * 获取单例
		 * 
		 */
		public static function getInstance():MillionStupaUI
		{
			if(_self==null)
			{
				_self = new MillionStupaUI();
			}
			return _self;
		}
		
		public function MillionStupaUI(popUI:Boolean=true)
		{
			super(popUI);
			initEvent();
			initLoseGoods();
		}
		/**
		 *重写显示 
		 * @param space
		 * @param onshow
		 * 
		 */
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_TOWER);
			super.show(space,onshow);
		}
		/**
		 * 显示掉落物品 
		 * 
		 */
		private function initLoseGoods():void
		{
			loseGoods = new Array;
			for(var i:int=0;i<14;i++)
			{
				var good:LoseGoodsUI = new LoseGoodsUI;
				good.x = 7+(i%7)*56;
				good.y = 28+int(i/7)*56;
				loseGoods[i] = good;
				loseGoodsBG.addChild(loseGoods[i]);
			}
		}

		/**
		 * 事件初始化 
		 * 
		 */
		private function initEvent() :void
		{
			//_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_SHOW_TOWER,getFloor);
			chooseFloor.addEventListener(ComboBoxEvent.NXCOMBOBOX_CLICK,onChoose);
			challengeBtn.addEventListener(MouseEvent.CLICK,onChallenge);
		}
			
		/**
		 * 开始挑战
		 * 
		 */
		private function onChallenge(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_START_FIGHT,chooseFloor.index+1);
			if(challengeNum.htmlStrByBorder != "0")
				_self.hide();
		}
		/**
		 * 获取万灵塔楼层信息
		 */
		private function getFloor(evt:CustomToClientEvent):void
		{
			/*_self.show();
			if(evt.pList.shift() == 1)
			{
				isWinImg.visible = true;
			}
			evt.pList.shift();                      //玩家等级
			towerLevel = int(evt.pList.shift());    //当前可挑战的最高层数
			for(var j:int=0;j<towerLevel-1;j++)
			{
				isWin[j] = true;
			}
			challengeNum.htmlStr=evt.pList.shift(); //当前剩余挑战次数
			var length:int = evt.pList.length-1;   //万灵塔楼层数 
			if(0 != chooseFloor.dataList.length)
				chooseFloor.clear();
			for(var i:int=0;i<length/2;i++)        //将楼层信息加入选择框
			{
				var data:int   = evt.pList.shift();
				var str:String = "<font color='#"+(data>towerLevel?"77787B":"FFCB4A")+"'>"+StaticIni.getSwfLangStr(evt.pList.shift())+"</font>";
				chooseFloor.addItem({data:data,label:str});
			}
			if(chooseFloor.index == -1)
			{
				chooseFloor.index = evt.pList.shift()-1;
				onChoose(null);
			}
			chooseFloor.setSelected(nowChoose);*/
		}
		/**
		 * 选择某楼层，显示该楼层信息
		 * 
		 */
		private function onChoose(evt:ComboBoxEvent):void
		{
			nowChoose = chooseFloor.index;
			isWinImg.visible = isWin[nowChoose];
		
			//显示怪物头像
			challengeImg.source = ResConfig.pngPhotoPath+
				StaticIni.getIniVar("wlt_"+(chooseFloor.index+1),"pic");
			challengeImg.filters = nowChoose<towerLevel?null:[Filter.GRAY];
			
			//显示掉落物品
			var imgs:Array = StaticIni.getIniVar("wlt_"+(chooseFloor.index+1),"item").split(",");
			for(var i:int;i<imgs.length;i++)
			{
				LoseGoodsUI(loseGoods[i]).GoodImg = imgs[i];
			}
			for(var j:int=imgs.length;j<14;j++)
			{
				LoseGoodsUI(loseGoods[j]).GoodImg = "";
			}
		}
	}
}
